package com.dr.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dr.common.Const;
import com.dr.pojo.Constant;
import com.dr.pojo.Dept;
import com.dr.pojo.Regist;
import com.dr.pojo.User;
import com.dr.service.ConstantService;
import com.dr.service.DeptService;
import com.dr.service.RegistService;
import com.dr.service.UserService;
//@RestController 
@Controller
@RequestMapping("/um")
public class UserController { 
	@Autowired
	private UserService userService;//用户
	
	@Autowired
	private DeptService deptService; //科室
	
	@Autowired
	private ConstantService constantService; //职称
	
	@Autowired
	private RegistService registService; //挂号级别
	
	@RequestMapping("/userList")
	public ModelAndView userList(User user) {
		List<User> userList = userService.findUserList(); //取得用户列表数据
		ModelAndView mav = new ModelAndView();
		mav.addObject("userList", userList); //向页面传数据,相当于request.setAttribute("userList", uesrList)
		mav.setViewName("userManage/userList.html");
		
		return mav;
	}
	
	
	@RequestMapping("/toAdd")
	public ModelAndView toAdd() {
		//System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
		ModelAndView mav = new ModelAndView();
		List<Dept> deptList = deptService.findDeptList();
		List<Constant> constantList = constantService.findConstantList();
		List<Regist> registList = registService.findRegistList();
		mav.addObject("deptList", deptList); 
		mav.addObject("constantList", constantList); 
		mav.addObject("registList", registList); 
		mav.setViewName("userManage/userAdd.html");		
		return mav;
	}
	
	@ResponseBody //  ajax方式提交@RestController = @Controller + @ResponseBody
	@RequestMapping("/doAdd")
	public Map<String,Object> doAdd(User user) {	
		//System.out.println(user);
		String userName = user.getUserName();
		User addUser = userService.findUserByName(userName);
		Map<String,Object> map=new HashMap<>();
		if(addUser!=null) {
			//账户存在		
			//mav.addObject("attrInfoUserAdd","账户已存在");//硬编码
			map.put("attrInfoUserAdd",Const.INF_USER_NAME_EXIST);			
			map.put("status",Const.STATUS_FAILED);
		}
		else {
			//账户不存在
			int result = userService.insertUser(user);
			if(result>0) {
				map.put("status",Const.STATUS_SCUESS);
			}
			else {
				map.put("status",Const.STATUS_FAILED);
			}
		}	
		return map;
	}
	
	@RequestMapping("/toUpdate")
	public ModelAndView toUpdate(Integer id) {
		ModelAndView mav = new ModelAndView();
		User user=userService.findUserById(id);
		List<Dept> deptList = deptService.findDeptList();
		List<Constant> constantList = constantService.findConstantList();
		List<Regist> registList = registService.findRegistList();
		mav.addObject("user", user); 
		mav.addObject("deptList", deptList); 
		mav.addObject("constantList", constantList); 
		mav.addObject("registList", registList); 
		mav.setViewName("userManage/userUpdate.html");		
		return mav;
	}
	
	@ResponseBody //  ajax方式提交@RestController = @Controller + @ResponseBody
	@RequestMapping("/doUpdate")
	public Map<String,Object> doUpdate(User user) {	
			Map<String,Object> map=new HashMap<>();
			int result = userService.updateUser(user);
			if(result>0) {
				map.put("status",Const.STATUS_SCUESS);
			}
			else {
				map.put("status",Const.STATUS_FAILED);
			}	
		return map;
	}
	
	@ResponseBody
	@RequestMapping("/doDelete")
	public Map<String,Object> doDelete(Integer... chk) {	//相当于Integer[] chk
			Map<String,Object> map=new HashMap<>();
			int result = userService.deleteUser(chk);
			if(result>0) {
				map.put("status",Const.STATUS_SCUESS);
			}
			else {
				map.put("status",Const.STATUS_FAILED);
			}	
			return map;
	}
	
	//修改密码页面
	@RequestMapping("/toPassword")
	public ModelAndView toPassword(String userName) {
		ModelAndView mav = new ModelAndView(); 
		mav.addObject("userName", userName); 
		mav.setViewName("userManage/pwdUpdate.html");		
		return mav;
	}
	
	@ResponseBody
	@RequestMapping("/doPassword")
	public Map<String,Object> doPassword(User user) {	
			Map<String,Object> map=new HashMap<>();
			int result = userService.updatePassword(user);
			if(result>0) {
				map.put("status",Const.STATUS_SCUESS);
			}
			else {
				map.put("status",Const.STATUS_FAILED);
			}	
			return map;
	}
}
